package m1.server.composant.connectionmanager;

import m2.Composant;
import m2.Configuration;

public class ConnectionManager extends Composant {

	/**
	 * Permet de savoir dans quel �tat se trouve le ConnectionManager
	 */
	private int etat;
	/**
	 * Service fourni
	 */
	private SendRequestIntS sendRequestIntS;
	/**
	 * Service requis
	 */
	private ReceiveSecurityAuthS receiveSecurityAuthS;
	/**
	 * Service requis
	 */
	private ReceiveRequestDBS receiveRequestDBS;
	/**
	 * 
	 */
	private ExternalSocketRS externalSocketRS;
	/**
	 * 
	 */
	private ExternalSocketFS externalSocketFS;
	/**
	 * Service fourni
	 */
	private SendSecurityAuthS sendSecurityAuthS;

	public ConnectionManager(Configuration parent) {
		super("ConnectionManager", parent);

		etat = 0;

		externalSocketFS = new ExternalSocketFS();
		externalSocketFS.setComposant(this);
		addServiceFourni(externalSocketFS);
		externalSocketFS.getPortFourni().setComposant(this);
		addPortFournis(externalSocketFS.getPortFourni());

		externalSocketRS = new ExternalSocketRS();
		externalSocketRS.setComposant(this);
		addServiceRequis(externalSocketRS);
		externalSocketRS.getPortRequis().setComposant(this);
		addPortRequis(externalSocketRS.getPortRequis());

		receiveRequestDBS = new ReceiveRequestDBS();
		receiveRequestDBS.setComposant(this);
		addServiceFourni(receiveRequestDBS);
		receiveRequestDBS.getPortFourni().setComposant(this);
		addPortFournis(receiveRequestDBS.getPortFourni());

		receiveSecurityAuthS = new ReceiveSecurityAuthS();
		receiveSecurityAuthS.setComposant(this);
		addServiceRequis(receiveSecurityAuthS);
		receiveSecurityAuthS.getPortRequis().setComposant(this);
		addPortRequis(receiveSecurityAuthS.getPortRequis());

		sendRequestIntS = new SendRequestIntS();
		sendRequestIntS.setComposant(this);
		addServiceRequis(sendRequestIntS);
		sendRequestIntS.getPortRequis().setComposant(this);
		addPortRequis(sendRequestIntS.getPortRequis());

		sendSecurityAuthS = new SendSecurityAuthS();
		sendSecurityAuthS.setComposant(this);
		addServiceFourni(sendSecurityAuthS);
		sendSecurityAuthS.getPortFourni().setComposant(this);
		addPortFournis(sendSecurityAuthS.getPortFourni());
	}

	@Override
	public void update(Object o, Object arg) {

		switch (etat) {
			case 0:
				etat++;
				sendSecurityAuthS.activer(arg);
				break;
			case 1:
				etat++;
				if(arg.equals("!!")){
					externalSocketFS.activer(o);
				}
				receiveRequestDBS.activer(arg);
				break;
			case 2:
				etat = 0;
				externalSocketFS.activer(arg);
				break;
			default:;
		}
	}
}
